﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
using log4net;
using MPMovieCatalog.Domain;

namespace MPMovieCatalog.Util
{
    public class Utils
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Utils));

        private static List<string> tempFiles = new List<string>();

        public static string DownloadImage(string url)
        {
            string fileName = GetTempFileName();
            if (!string.IsNullOrEmpty(url))
            {
                try
                {
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                    webRequest.Timeout = 5000;
                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        byte[] inBuf = new byte[900000];
                        int bytesToRead = (int)inBuf.Length;
                        int bytesRead = 0;

                        DateTime dt = DateTime.Now;
                        while (bytesToRead > 0)
                        {
                            dt = DateTime.Now;
                            int n = stream.Read(inBuf, bytesRead, bytesToRead);
                            if (n == 0)
                                break;
                            bytesRead += n;
                            bytesToRead -= n;
                            TimeSpan ts = DateTime.Now - dt;
                            if (ts.TotalSeconds >= 5)
                            {
                                throw new System.Exception("timeout");
                            }
                        }
                        using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            fileStream.Write(inBuf, 0, bytesRead);
                            stream.Close();
                            fileStream.Close();
                        }
                    }

                }
                catch (System.Exception ex)
                {
                    log.Error(string.Format("Error downloading image from \"{0}\" to file \"{1}\"", url, fileName), ex);
                }
            }
            return fileName;
        }

        public static string GetTempFileName()
        {
            string fileName = Path.GetTempFileName();
            tempFiles.Add(fileName);

            return fileName;
        }

        public static void DeleteTempFiles()
        {
            foreach (string fileName in tempFiles)
            {
                try
                {
                    File.Delete(fileName);
                }
                catch (System.Exception ex)
                {
                    log.Error(string.Format("Error deleting file {0}", fileName), ex);
                }
            }
        }
    }
}
